const {
    PointRule,
    PointDetail,
    sequelize
    } = require('../models')
const fdnServer = require('../service/foundation')

exports.checkSnapshot = async (values) => {
    let changeList = []
    if (values.length === 0) {
        return changeList
    }
    const snapshot = values.pop()
    const jsondata = snapshot.jsondata
    const snapshots = values
    const len = snapshots.length
    //console.log(len)
    if (len === 0) {
        return changeList
    }
    if (snapshot.action === 'accepted') {
        for (let item in jsondata) {
            let status = {}
            for (let i = 0; i < len; i++) {
                let _jsondata = snapshots[i].jsondata
                if (jsondata[item] === _jsondata[item]) {
                    if (i === len - 1) {
                        continue
                    }
                    status.rule_name = `accepted.${item}`
                    status.item_id = snapshot.module_itemId.split('_')[1]
                    status.log_time = snapshot.createdAt
                    status.user_id = snapshots[i].createdBy
                    status.relation_user_id = snapshots[len - 1].createdBy
                    status.relation_log_time = snapshots[len - 1].createdAt
                    for (let j = 1; j < len; j++) {
                        if (snapshots[len - 1 - j].jsondata[item] === snapshots[len - 1].jsondata[item]) {
                            status.relation_user_id = snapshots[len - 1 - j].createdBy
                            status.relation_log_time = snapshots[len - 1 - j].createdAt
                        } else {
                            break
                        }
                    }

                    break
                }
            }
            if (Object.keys(status).length > 0) {
                changeList.push(status)
            }
        }
    } else {
        for (let item in jsondata) {
            let status = {}
            let temp
            for (let i = len - 1; i >= 0; i--) {
                let _jsondata = snapshots[i].jsondata
                if (i === len - 1) {
                    if (jsondata[item] === _jsondata[item])continue
                    status.ruleName = `modify.${item}`
                    if (len === 1) {
                        status.ruleName = `created.${item}`
                    }
                    status.item_id = snapshot.module_itemId.split('_')[1]
                    status.log_time = snapshot.createdAt
                    status.user_id = snapshot.createdBy
                    status.relation_user_id = snapshots[i].createdBy
                    status.relation_log_time = snapshots[i].createdAt
                    temp = _jsondata[item]
                }
                if (temp === _jsondata[item]) {
                    if (i === 0) {
                        status.rule_name = `created.${item}`
                    }
                    status.relation_user_id = snapshots[i].createdBy
                    status.relation_log_time = snapshots[i].createdAt
                }
                if(temp !== _jsondata[item]) {
                    temp = null
                }
            }
            if (Object.keys(status).length > 0) {
                changeList.push(status)
            }

        }
    }
    return changeList
}


exports.pointProcess = async (rulelist) => {
    //console.log(rulelist.length)
    let rlt = []
    for (let i = 0; i < rulelist.length; i++) {
        let createRuleDetail
        const rule = await PointRule.findOne({
            where: {
                name: rulelist[i].ruleName,
                is_valid: 1
            }
        })
        if (!rule) {
            continue
        }
        if (rule.type_id === 1) {
            //create 方式
            createRuleDetail = await PointDetail.create({
                user_id: rulelist[i].user_id,
                rule_id: rule.id,
                alteration: rule.alteration,
                item_id: rulelist[i].item_id,
                log_time: rulelist[i].log_time,
                relation_user_id: rulelist[i].relation_user_id,
                relation_alteration: 0,
                relation_log_time: rulelist[i].relation_log_time
            })

            await fdnServer.pointUpdate(rule.alteration,rulelist[i].user_id,'add')
            //await UserPoint.update({integral: sequelize.literal(`integral + ${rule.alteration}`)}, {where: {userId: rulelist[i].userId}}, {transaction})



        }
        if (rule.type_id === 2) {
            // 积分兑换 方式
            const userPoint = await fdnServer.getPoint(rulelist[i].user_id)

            if (userPoint.total.sophic - rule.alteration < 0) {
                createRuleDetail = []
                return createRuleDetail
            }
            createRuleDetail = await PointDetail.$create({
                item_id: 0, //@todo 无设备Id. 设置为0
                user_id: rulelist[i].user_id,
                rule_id: rule.id,
                alteration: rule.alteration,
            })
            await fdnServer.pointUpdate(rule.alteration, rulelist[i].user_id, 'sub')
            //await UserPoint.update({integral: sequelize.literal(`integral - ${rule.alteration}`)}, {where: {userId: rulelist[i].userId}}, {transaction})

        }
        if (rule.type_id === 3) {
            //修改字段
            if (rulelist[i].user_id === rulelist[i].relation_user_id) {
                continue
            }
            createRuleDetail = await PointDetail.$create({
                user_id: rulelist[i].user_id,
                rule_id: rule.id,
                alteration: rule.alteration,
                item_id: rulelist[i].item_id,
                log_time: rulelist[i].log_time,
                relation_user_id: rulelist[i].relation_user_id,
                relation_alteration: rule.alteration,
                relation_log_time: rulelist[i].relation_log_time
            })
            //console.log(rulelist[i].user_id)
            //@todo diffirent coefficientType with diffirent deals
            await fdnServer.pointUpdate(rule.alteration,rulelist[i].user_id,'add')
            await fdnServer.pointUpdate(rule.alteration,rulelist[i].relation_user_id,'sub')
            //await UserPoint.update({integral: sequelize.literal(`integral + ${rule.alteration}`)}, {where: {userId: rulelist[i].userId}}, {transaction})
            //await UserPoint.update({integral: sequelize.literal(`integral - ${rule.alteration}`)}, {where: {userId: rulelist[i].relationUserId}}, {transaction})

        }
        if (rule.type_id === 4) {
            //申诉成功  复检修改
            if (rulelist[i].user_id === rulelist[i].relation_user_id) {
                continue
            }
            createRuleDetail = await PointDetail.$create({
                user_id: rulelist[i].user_id,
                rule_id: rule.id,
                alteration: rule.alteration,
                item_id: rulelist[i].item_id,
                log_time: rulelist[i].log_time,
                relation_user_id: rulelist[i].relation_user_id,
                relation_alteration: rule.alteration,
                relation_log_time: rulelist[i].relation_log_time
            })
            await fdnServer.pointUpdate(rule.alteration, rulelist[i].user_id, 'add')
            await fdnServer.pointUpdate(rule.alteration, rulelist[i].relation_user_id, 'sub')
            //await UserPoint.update({integral: sequelize.literal(`integral + ${rule.alteration}`)}, {where: {userId: rulelist[i].userId}}, {transaction})
            //await UserPoint.update({integral: sequelize.literal(`integral - ${rule.alteration}`)}, {where: {userId: rulelist[i].relationUserId}}, {transaction})

        }
        if (createRuleDetail) {
            rlt.push(createRuleDetail)
        }

    }
    return rlt
}
